package com.blockchain.console.service.common.achieve;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.blockchain.common.enums.IsPass;
import com.blockchain.console.service.common.IndexManage;
import com.blockchain.console.service.common.entity.IndexCount;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.variables.P2PConst;

public class IndexManageImpl extends AbstractCommonService implements IndexManage {

	public static class IndexManageFactory implements ServiceFactory<IndexManage> {

		@Override
		public IndexManage newInstance(ServiceResource serviceResource) {
			return new IndexManageImpl(serviceResource);
		}
	}

	public IndexManageImpl(ServiceResource serviceResource) {
		super(serviceResource);

	}

	@Override
	public boolean isOneLogin() throws Throwable {
		boolean isOneLogin = false;
		try (Connection connection = getConnection()) {
			try (PreparedStatement ps = connection.prepareStatement("SELECT F09 FROM T7011 WHERE F01=?")) {
				ps.setInt(1, serviceResource.getSession().getAccountId());
				try (ResultSet rs = ps.executeQuery()) {
					if (rs.next()) {
						IsPass isPass = EnumParser.parse(IsPass.class, rs.getString(1));
						if (isPass == IsPass.S) {
							isOneLogin = true;
						}
					}
				}
			}
		}
		return isOneLogin;
	}

	@Override
	public IndexCount getIndexCount() throws Throwable {
		IndexCount index = new IndexCount();
		try (Connection conn = getConnection()) {
			try (PreparedStatement pst = conn
					.prepareStatement("SELECT F01, F02, F03, F04, F05, F06,F07, F08, F09,F10,F11,F12 FROM T7010")) {
				try (ResultSet rst = pst.executeQuery()) {
					if (rst.next()) {
						index.todayRegisterUser = rst.getInt(1);
						index.historyRegisterUser = rst.getInt(2);
						index.todayLoginUser = rst.getInt(3);
						index.todayTotalUserRecharge = rst.getBigDecimal(4);
						index.historyTotalUserRecharge = rst.getBigDecimal(5);
						index.todayTotalUserExtract = rst.getBigDecimal(6);
						index.historyTotalUserExtract = rst.getBigDecimal(7);
						index.platformTotalIncome = rst.getBigDecimal(8);
						index.platformZzc = rst.getBigDecimal(9);
						index.jrPlatformTotalIncome = rst.getBigDecimal(10);
						index.jrPlatformZzc = rst.getBigDecimal(11);
						index.gxsj = rst.getTimestamp(12);
					}
				}
			}
		}
		return index;
	}

	@Override
	public BigDecimal getBtcTotal() throws Throwable{
		String sql = "select sum(t6025.f04+t6025.f05) from "+P2PConst.DB_USER+".t6025 t6025 LEFT JOIN "+P2PConst.DB_USER+".t6013 t6013 on t6025.f03 = t6013.f01 where t6013.f02='btc';";
		return getOneStr(sql);
		
	}

	@Override
	public BigDecimal getTradeTotal() throws Throwable{
		String sql = "select sum(f07)/2 from "+P2PConst.DB_USER+".t6018;";
		return getOneStr(sql);
	}

	@Override
	public BigDecimal getArticalTotal() throws Throwable{
		String sql = "select count(f01) from "+P2PConst.DB_INFORMATION+".t5011;";
		return getOneStr(sql);
	}
	
	private BigDecimal getOneStr(String sql) throws Throwable{
		BigDecimal res = null;
		try (Connection conn = getConnection()) {
			try (PreparedStatement pst = conn
					.prepareStatement(sql)) {
				try (ResultSet rst = pst.executeQuery()) {
					if (rst.next()) {
						res = rst.getBigDecimal(1);
					}
				}
			}
		}
		return res==null?new BigDecimal(0):res;
	}
	
}
